package com.irate.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.irate.activity.R;
import com.irate.model.RatingCriteria;
import com.irate.provider.ContentManager;

public class RatingCriteriaAdapter extends BaseAdapter {

	public RatingCriteriaAdapter(Context ct) {
	}

	@Override
	public int getCount() {

		return ContentManager.getInstance().getListRatingCriteria().size();
	}

	@Override
	public Object getItem(int arg0) {

		return ContentManager.getInstance().getListRatingCriteria().get(arg0);
	}

	@Override
	public long getItemId(int arg0) {

		return Integer.parseInt(ContentManager.getInstance()
				.getListRatingCriteria().get(arg0).getCat_id());
	}

	static class ViewHolder {
		TextView tv_title;
		TextView rating_lvl_one;
		TextView rating_lvl_two;
		TextView rating_lvl_three;
		TextView rating_lvl_four;
		TextView rating_lvl_five;
		TextView rating_lvl_six;

		int position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final RatingCriteria criteria = (RatingCriteria) getItem(position);

		if (convertView == null) {
			LayoutInflater inflat = LayoutInflater.from(parent.getContext());
			convertView = inflat.inflate(R.layout.item_criteria, null);

			TextView tv = (TextView) convertView
					.findViewById(R.id.rating_criteria_tile);

			tv.setPadding(15, 15, 15, 15);

			final TextView rating_lvl_one = (TextView) convertView
					.findViewById(R.id.rating_lvl_one);
			final TextView rating_lvl_two = (TextView) convertView
					.findViewById(R.id.rating_lvl_two);
			final TextView rating_lvl_three = (TextView) convertView
					.findViewById(R.id.rating_lvl_three);
			final TextView rating_lvl_four = (TextView) convertView
					.findViewById(R.id.rating_lvl_four);
			final TextView rating_lvl_five = (TextView) convertView
					.findViewById(R.id.rating_lvl_five);
			final TextView rating_lvl_six = (TextView) convertView
					.findViewById(R.id.rating_lvl_six);

			ViewHolder vh = new ViewHolder();
			vh.tv_title = tv;
			vh.rating_lvl_one = rating_lvl_one;
			vh.rating_lvl_two = rating_lvl_two;
			vh.rating_lvl_three = rating_lvl_three;
			vh.rating_lvl_four = rating_lvl_four;
			vh.rating_lvl_five = rating_lvl_five;
			vh.rating_lvl_six = rating_lvl_six;

			convertView.setTag(vh);
		}

		final ViewHolder viewSaver = (ViewHolder) convertView.getTag();
		viewSaver.tv_title.setText(criteria.getName());

		switch (criteria.getRate()) {
		case 1:
			lvlone(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
					viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
					viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);

			break;
		case 2:
			lvltwo(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
					viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
					viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			break;
		case 3:
			lvlthree(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
					viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
					viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			break;
		case 4:
			lvlfour(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
					viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
					viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			break;
		case 5:
			lvlfive(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
					viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
					viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			break;
		case 6:
			lvlsix(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
					viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
					viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			break;
		default:
			lvlInvalid(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
					viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
					viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			break;
		}

		viewSaver.rating_lvl_one.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				criteria.setRate(1);
				lvlone(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
						viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
						viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			}

		});

		viewSaver.rating_lvl_two.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				criteria.setRate(2);
				lvltwo(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
						viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
						viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			}
		});
		viewSaver.rating_lvl_three.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				criteria.setRate(3);
				lvlthree(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
						viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
						viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			}
		});
		viewSaver.rating_lvl_four.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				criteria.setRate(4);
				lvlfour(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
						viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
						viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			}
		});
		viewSaver.rating_lvl_five.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				criteria.setRate(5);
				lvlfive(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
						viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
						viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			}
		});
		viewSaver.rating_lvl_six.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				criteria.setRate(6);
				lvlsix(viewSaver.rating_lvl_one, viewSaver.rating_lvl_two,
						viewSaver.rating_lvl_three, viewSaver.rating_lvl_four,
						viewSaver.rating_lvl_five, viewSaver.rating_lvl_six);
			}
		});

		convertView.setBackgroundDrawable(parent.getContext().getResources()
				.getDrawable(R.drawable.category_item_background));

		return convertView;
	}

	private void lvlInvalid(TextView rating_lvl_one, TextView rating_lvl_two,
			TextView rating_lvl_three, TextView rating_lvl_four,
			TextView rating_lvl_five, TextView rating_lvl_six) {
		rating_lvl_one.setBackgroundResource(R.drawable.circle);
		rating_lvl_one.setText("1");

		rating_lvl_two.setBackgroundResource(R.drawable.circle);
		rating_lvl_two.setText("2");

		rating_lvl_three.setBackgroundResource(R.drawable.circle);
		rating_lvl_three.setText("3");

		rating_lvl_four.setBackgroundResource(R.drawable.circle);
		rating_lvl_four.setText("4");

		rating_lvl_five.setBackgroundResource(R.drawable.circle);
		rating_lvl_five.setText("5");

		rating_lvl_six.setBackgroundResource(R.drawable.circle);
		rating_lvl_six.setText("6");
	}

	private void lvlone(TextView rating_lvl_one, TextView rating_lvl_two,
			TextView rating_lvl_three, TextView rating_lvl_four,
			TextView rating_lvl_five, TextView rating_lvl_six) {
		rating_lvl_one.setBackgroundResource(R.drawable.motion1);
		rating_lvl_one.setText("");

		rating_lvl_two.setBackgroundResource(R.drawable.circle);
		rating_lvl_two.setText("2");

		rating_lvl_three.setBackgroundResource(R.drawable.circle);
		rating_lvl_three.setText("3");

		rating_lvl_four.setBackgroundResource(R.drawable.circle);
		rating_lvl_four.setText("4");

		rating_lvl_five.setBackgroundResource(R.drawable.circle);
		rating_lvl_five.setText("5");

		rating_lvl_six.setBackgroundResource(R.drawable.circle);
		rating_lvl_six.setText("6");
	}

	private void lvltwo(TextView rating_lvl_one, TextView rating_lvl_two,
			TextView rating_lvl_three, TextView rating_lvl_four,
			TextView rating_lvl_five, TextView rating_lvl_six) {
		rating_lvl_one.setBackgroundResource(R.drawable.motion2);
		rating_lvl_one.setText("");

		rating_lvl_two.setBackgroundResource(R.drawable.motion2);
		rating_lvl_two.setText("");

		rating_lvl_three.setBackgroundResource(R.drawable.circle);
		rating_lvl_three.setText("3");

		rating_lvl_four.setBackgroundResource(R.drawable.circle);
		rating_lvl_four.setText("4");

		rating_lvl_five.setBackgroundResource(R.drawable.circle);
		rating_lvl_five.setText("5");

		rating_lvl_six.setBackgroundResource(R.drawable.circle);
		rating_lvl_six.setText("6");

	}

	private void lvlthree(TextView rating_lvl_one, TextView rating_lvl_two,
			TextView rating_lvl_three, TextView rating_lvl_four,
			TextView rating_lvl_five, TextView rating_lvl_six) {
		rating_lvl_one.setBackgroundResource(R.drawable.motion3);
		rating_lvl_one.setText("");

		rating_lvl_two.setBackgroundResource(R.drawable.motion3);
		rating_lvl_two.setText("");

		rating_lvl_three.setBackgroundResource(R.drawable.motion3);
		rating_lvl_three.setText("");

		rating_lvl_four.setBackgroundResource(R.drawable.circle);
		rating_lvl_four.setText("4");

		rating_lvl_five.setBackgroundResource(R.drawable.circle);
		rating_lvl_five.setText("5");

		rating_lvl_six.setBackgroundResource(R.drawable.circle);
		rating_lvl_six.setText("6");

	}

	private void lvlfour(TextView rating_lvl_one, TextView rating_lvl_two,
			TextView rating_lvl_three, TextView rating_lvl_four,
			TextView rating_lvl_five, TextView rating_lvl_six) {
		rating_lvl_one.setBackgroundResource(R.drawable.motion4);
		rating_lvl_one.setText("");

		rating_lvl_two.setBackgroundResource(R.drawable.motion4);
		rating_lvl_two.setText("");

		rating_lvl_three.setBackgroundResource(R.drawable.motion4);
		rating_lvl_three.setText("");

		rating_lvl_four.setBackgroundResource(R.drawable.motion4);
		rating_lvl_four.setText("");

		rating_lvl_five.setBackgroundResource(R.drawable.circle);
		rating_lvl_five.setText("5");

		rating_lvl_six.setBackgroundResource(R.drawable.circle);
		rating_lvl_six.setText("6");

	}

	private void lvlfive(TextView rating_lvl_one, TextView rating_lvl_two,
			TextView rating_lvl_three, TextView rating_lvl_four,
			TextView rating_lvl_five, TextView rating_lvl_six) {
		rating_lvl_one.setBackgroundResource(R.drawable.motion5);
		rating_lvl_one.setText("");

		rating_lvl_two.setBackgroundResource(R.drawable.motion5);
		rating_lvl_two.setText("");

		rating_lvl_three.setBackgroundResource(R.drawable.motion5);
		rating_lvl_three.setText("");

		rating_lvl_four.setBackgroundResource(R.drawable.motion5);
		rating_lvl_four.setText("");

		rating_lvl_five.setBackgroundResource(R.drawable.motion5);
		rating_lvl_five.setText("");

		rating_lvl_six.setBackgroundResource(R.drawable.circle);
		rating_lvl_six.setText(6 + "");

	}

	private void lvlsix(TextView rating_lvl_one, TextView rating_lvl_two,
			TextView rating_lvl_three, TextView rating_lvl_four,
			TextView rating_lvl_five, TextView rating_lvl_six) {
		rating_lvl_one.setBackgroundResource(R.drawable.motion6);
		rating_lvl_one.setText("");

		rating_lvl_two.setBackgroundResource(R.drawable.motion6);
		rating_lvl_two.setText("");

		rating_lvl_three.setBackgroundResource(R.drawable.motion6);
		rating_lvl_three.setText("");

		rating_lvl_four.setBackgroundResource(R.drawable.motion6);
		rating_lvl_four.setText("");

		rating_lvl_five.setBackgroundResource(R.drawable.motion6);
		rating_lvl_five.setText("");

		rating_lvl_six.setBackgroundResource(R.drawable.motion6);
		rating_lvl_six.setText("");

	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return false;
	}

}
